package com.ape.mapsa.common;

import javax.el.MethodExpression;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.MethodExpressionActionListener;

import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;

public abstract class BeanCRUD {

	protected MenuModel menuModel;
	protected Submenu newSm;
	protected Submenu archivo;
	protected Submenu edicion;

	public BeanCRUD() {
		menuModel = new DefaultMenuModel();
		archivo = new Submenu();
		archivo.setLabel("Archivo");
		archivo.setId("fileSubMenu");
		archivo.setIcon("ui-icon-document");

		MenuItem mi = new MenuItem();
		mi.setIcon("ui-icon-pencil");
		mi.setId("editId");
		mi.setValue("Editar");
		mi.setProcess("@this");
		FacesContext context = FacesContext.getCurrentInstance();
		MethodExpression edit = context.getApplication().getExpressionFactory()
				.createMethodExpression(context.getELContext(), "#{cc.attrs.crudManagedBean.edit}", null, new Class[] { ActionEvent.class });
		mi.addActionListener(new MethodExpressionActionListener(edit));
		archivo.getChildren().add(mi);

		mi = new MenuItem();
		mi.setIcon("ui-icon-document");
		mi.setId("newId");
		mi.setProcess("@this");
		mi.setValue("Nuevo");
		MethodExpression create = context.getApplication().getExpressionFactory()
				.createMethodExpression(context.getELContext(), "#{cc.attrs.crudManagedBean.create}", null, new Class[] { ActionEvent.class });
		mi.addActionListener(new MethodExpressionActionListener(create));
		archivo.getChildren().add(mi);

		menuModel.addSubmenu(archivo);

		edicion = new Submenu();
		edicion.setLabel("Edicion");
		edicion.setId("editSmId");
		edicion.setIcon("ui-icon-pencil");

		mi = new MenuItem();
		mi.setIcon("ui-icon-save");
		mi.setId("saveId");
		mi.setValue("Guardar");
		MethodExpression saveAl = context.getApplication().getExpressionFactory()
				.createMethodExpression(context.getELContext(), "#{cc.attrs.crudManagedBean.save}", null, new Class[] { ActionEvent.class });
		mi.addActionListener(new MethodExpressionActionListener(saveAl));
		edicion.getChildren().add(mi);

		mi = new MenuItem();
		mi.setIcon("ui-icon-arrowrefresh-1-w");
		mi.setId("updateMenuId");
		mi.setValue("Actualizar");
		MethodExpression actionListener = context.getApplication().getExpressionFactory()
				.createMethodExpression(context.getELContext(), "#{cc.attrs.crudManagedBean.update}", null, new Class[] { ActionEvent.class });
		mi.addActionListener(new MethodExpressionActionListener(actionListener));

		edicion.getChildren().add(mi);

		mi = new MenuItem();
		mi.setIcon("ui-icon-trash");
		mi.setId("deleteMenuId");
		mi.setValue("Desactivar");
		MethodExpression desactivarAL = context.getApplication().getExpressionFactory()
				.createMethodExpression(context.getELContext(), "#{cc.attrs.crudManagedBean.delete}", null, new Class[] { ActionEvent.class });
		mi.addActionListener(new MethodExpressionActionListener(desactivarAL));

		edicion.getChildren().add(mi);

		menuModel.addSubmenu(edicion);

		menuModel.addSubmenu(edicion);

	}

	public Submenu getNewSm() {
		return newSm;
	}

	public void setNewSm(Submenu newSm) {
		this.newSm = newSm;
	}

	public abstract void edit(ActionEvent event);

	public abstract void save(ActionEvent event);

	public abstract void update(ActionEvent event);

	public abstract void create(ActionEvent event);

	public abstract void delete(ActionEvent event);

	public abstract MenuModel getMenuModel();
}
